// pages/activity/activity.js
const getToken = require('../../utils/get_token.js');
let cookie, token,cId,kid,kin;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  getBanner(that);
  if(cId==1){
    indexList(that)//初始加载
  }else{
    goodList(that)
  }
}
const getBanner = (that) => {//轮播图SLP_TOP_AD/
  let params = [ 'shop/pptIco/lorTest', {"A":kid,"B":kin}]
  getToken.getData(function (res) {
    console.log(res.data)
    let data = res.data;
    that.data.img=data.A[0];
    that.data.img.I='http://www.globalhexi.com/'+that.data.img.I;
    that.setData({
      img: that.data.img
    })
  }, ...params)
};
const indexList = (that) => {//商品列表
  if (that.data.boolT) {
    that.setData({hidden: false})
    that.data.boolT = false;
    let params = ['shop/pingTuan/indexQ99G5', {"pageNo":that.data.pNo,"isType":2}]
    getToken.getData(function (res) {
      let data = res.data;
      if(data.rows.length>0)
			{
				for(let i=0;i<data.rows.length;i++)
				{   
					data.rows[i].goodsImageStore=JSON.parse(data.rows[i].goodsImageStore);
					data.rows[i].img='http://www.globalhexi.com/'+data.rows[i].goodsImageStore[0].path+'/small/'+data.rows[i].goodsImageStore[0].id+'.jpg';
					data.rows[i].intPinPrice=(data.rows[i].intPinPrice/100).toFixed(2);
					data.rows[i].intPrice=(data.rows[i].intPrice/100).toFixed(2);
          that.data.arr.push(data.rows[i]);
				}
				that.data.pNo++;			  	 
			} 
      setTimeout(function () {
        that.data.boolT = true;
        that.setData({
          arr: that.data.arr,
          hidden: true
        })
      }, 500)
    }, ...params)
  }
};
const goodList = (that) => {//商品列表
  if (that.data.boolT) {
    that.setData({hidden: false})
    that.data.boolT = false;
    let params = ['shop/pingTuan/indexQ99G5', { 'pageNo': that.data.pNo, 'isType': 5, 'categoryId': cId }]
    getToken.getData(function (res) {
      let data = res.data;
      if(data.rows.length>0)
			{
				for(let i=0;i<data.rows.length;i++)
				{   
					data.rows[i].goodsImageStore=JSON.parse(data.rows[i].goodsImageStore);
					data.rows[i].img='http://www.globalhexi.com/'+data.rows[i].goodsImageStore[0].path+'/small/'+data.rows[i].goodsImageStore[0].id+'.jpg';
					data.rows[i].intPinPrice=(data.rows[i].intPinPrice/100).toFixed(2);
					data.rows[i].intPrice=(data.rows[i].intPrice/100).toFixed(2);
          that.data.arr.push(data.rows[i]);
				}
				that.data.pNo++;			  	 
			} 
      setTimeout(function () {
        that.data.boolT = true;
        that.setData({
          arr: that.data.arr,
          hidden: true
        })
      }, 500)
    }, ...params)
  }
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    boolT: true,
    img:"",
    pNo:1,
    arr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    kid='SLP_NCP_TOP_AD';
    kin='';
    cId=options.id;
    if(options.kid!=1){
      kid=options.kid;
      kin=options.kin;
    }
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    ckToken(that);
  },
  go_detail: function (e) {//直接去详情
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if(cId==1){
      indexList(that)//初始加载
    }else{
      goodList(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})